package com.yanqu.road.dao.impl.taobao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import taobao.config.TaoBaoPriceConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TaoBaoPriceConfigDaoImpl extends TempDao {

    public static String sql = "select * from t_s_activity_taobao_price where activityId = ?";

    public Map<Integer, TaoBaoPriceConfig> getConfigMap(int activityId) {
        Map<Integer, TaoBaoPriceConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TaoBaoPriceConfig config = new TaoBaoPriceConfig();
                config.setActivityId(rs.getInt("activityId"));
                config.setPriceId(rs.getInt("priceId"));
                config.setItemId(rs.getInt("itemId"));
                config.setSellPrice(rs.getInt("sellPrice"));
                config.setWeight(rs.getInt("weight"));
                config.setVipLimit(rs.getInt("vipLimit"));
                map.put(config.getPriceId(), config);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
